package com.cafe.service.impl;

import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cafe.dao.ProductDao;
import com.cafe.model.Product;
import com.cafe.service.ProductService;
import com.malamute.core.common.Page;
import com.malamute.core.util.ArrayUtil;

/**
 * @description 菜谱业务实现
 * @version v1.0
 */
@Service
public class ProductServiceImpl implements ProductService {
	@Autowired
	private ProductDao productDao;

	/*
	 * (non-Javadoc)
	 * 
	 * @param menu
	 * 
	 * @see com.cafe.service.MenuService#saveMenu(com.cafe.model.Menu)
	 */
	@Override
	public void saveProduct(Product product) {
		productDao.save(product);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @param menu
	 * 
	 * @see com.cafe.service.MenuService#updateMenu(com.cafe.model.Menu)
	 */
	@Override
	public void updateProduct(Product product) {
		productDao.update(product);

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @param id
	 * 
	 * @return
	 * 
	 * @see com.cafe.service.MenuService#getMenuById(java.lang.Integer)
	 */
	@Override
	public Product getProductById(Integer id) {
		return productDao.get(id);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @param id
	 * 
	 * @see com.cafe.service.MenuService#deleteMenuById(java.lang.Integer)
	 */
	@Override
	public void deleteProductById(Integer id) {
		productDao.deleteByKey(id);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @param criteria
	 * 
	 * @param page
	 * 
	 * @see
	 * com.cafe.service.MenuService#listMenusByPage(org.hibernate.criterion.
	 * DetachedCriteria, com.malamute.core.common.Page)
	 */
	@Override
	public void listProductsByPage(DetachedCriteria criteria, Page page) {
		productDao.listByPage(criteria, page);

	}

	@Override
	public List listProductsByIds(DetachedCriteria criteria) {
		return productDao.findByCriteria(criteria);
	}

	@Override
	public void soldOutProducts(List<Integer> productIds) {
		productDao
		.bulkUpdate("update from  Product p set p.isSoldOut=true where p.id in ("
				+ ArrayUtil.parseToString(productIds, ",") + ") ");
	}

	@Override
	public void onSellProducts(List<Integer> productIds) {
		productDao
				.bulkUpdate("update from  Product p set p.isSoldOut=false where p.id in ("
						+ ArrayUtil.parseToString(productIds, ",") + ") ");
	}

}
